from dataclasses import dataclass
import pandas as pd
import numpy as np

@dataclass
class Data2Grade:

    data:pd.DataFrame = None
    bins:int = None
    
    def __post_init__(self):
        for col in self.data.columns:
            cut_list = self.get_bins(self.data[col])
            self.data[f"{col}_{'grade'}"] = pd.cut(self.data[col], bins=cut_list, labels=range(len(cut_list)-1))
            
    
    def get_bins(self, columns: pd.core.series.Series):
        try:
            return sorted(set(np.percentile(columns.dropna(), np.arange(0, 100, 100 / self.bins))[1:].tolist() + [-np.Inf, np.Inf]))
        except:
            return [-np.Inf, np.Inf]
    
    